package in.muton.edushare.pages.univ;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.components.images.ProfileImage;
import in.muton.edushare.helper.TinyMCEHelper;
import in.muton.edushare.panels.upload.single.SingleUploadButtonPanel;
import in.muton.edushare.url.EntityPageParams;

import java.util.Arrays;
import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.FileDirectoriesDAO;
import com.edushare.common.file.image.ProfileImages;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.FileDirectories;
import com.edushare.common.vo.FilesUploaded;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.UserRoles;

public class UnivEdit extends UnivBase {

	public UnivEdit(PageParameters params) {
		super(params);
		prc.check(Arrays.asList(UserRoles.ADMIN_ROLE));		
		Form<Void> f = new Form<Void>("form");
		add(f);
		f.add(new TextField<String>("name", new PropertyModel<String>(super.getModel(), "universityName")));
		f.add(
				new TextArea<String>(
										"desc", 
										new PropertyModel<String>(super.getModel(), "universityDesc"))
											.add(TinyMCEHelper.getDefaultTinyMCEBehavior())
			);
		final ProfileImage pI = new ProfileImage("img", super.getModel(), ApplicationConstants.FILE_SUFFIX_HBADGE);
		pI.setOutputMarkupId(true);
		f.add(pI);
		SingleUploadButtonPanel uploadButton = new SingleUploadButtonPanel
		
								("imgUpload", new PropertyModel<FileDirectories>(UnivEdit.this, "univGallery")) 
										{
											
											private static final long serialVersionUID = 2281037462277939285L;
								
											@Override
											protected void onUploadWindowClose(AjaxRequestTarget target) {
												List<FilesUploaded> lf = EduShareSession.get().getFileUploadedInLastUpload();
//												FilesUploaded fu = null;
												if(lf!=null && lf.size()>0){
													FilesUploaded fu = lf.get(lf.size()-1);//last item uploaded
													if(fu!=null){
														getModel().getObject().setImageFile(fu);
														getSessionORMInterface().commitAllChanges();
													}
												}
												target.addComponent(pI);
											}
										};
		f.add(uploadButton);
		f.add(new Button("submit"){
			
			private static final long serialVersionUID = -8273424307820510119L;

			@Override
			public void onSubmit() {
				Universities u = UnivEdit.this.getModel().getObject();
				if(u.getName()!=null && u.getName().length() > ApplicationConstants.MAX_TEXT_FIELD_SIZE){
					setErrorMessage("University name is too long.");
					return;
				}else if(u.getUniversityDesc()!=null && u.getUniversityDesc().length()>ApplicationConstants.MAX_DESC_SIZE){
					setErrorMessage("University Description is too long");
					return;
				}else if(!StringUtil.isValid(u.getName())){
					setErrorMessage("University name is required");
					return;
				}
				
				clearError();
				getSessionORMInterface().commitAllChanges();
				setResponsePage(UnivPage.class, EntityPageParams.getParams(UnivEdit.this.getModel()));
			}
		});
		
		add(f);
	}

	@Override
	public String getActivePage(PageParameters p) {
		return "editUniv";
	}

	@Override
	public String getPageTitle() {
		return "Edit University | "+ ApplicationConstants.APPLICATION_NAME;
	}
	
	public FileDirectories getUnivGallery(){
		FileDirectories fd = new FileDirectoriesDAO(getSessionORMInterface()).getOrCreateDefaultGallery(super.getModel().getObject());
		fd.setFileProcessStrategy(new ProfileImages());
		return fd;
	}

}
